<template>
  <div class="bodyBox">
    <FrameStyle>
      <!-- 面包屑 -->
      <Breadcrumb :breadName="`用户管理`" slot="breadcrumb"/>
      <!-- 搜索框 -->
      <el-input slot="search" @input="searchData" v-model="searchVal">
        <el-button class="el-icon-search" slot="append"></el-button>
      </el-input>
      <!-- 添加按钮 -->
      <el-button type="primary" slot="addBtn" @click="flag = true"
        >添加用户</el-button
      >
      <!-- 表格列表 -->
      <el-table :data="tableData" stripe slot="tableList" height="370px">
        <el-table-column label="创建时间" prop="createTime" width="120px">
        </el-table-column>
        <el-table-column prop="username" label="账号"></el-table-column>
        <el-table-column prop="password" label="密码"></el-table-column>
        <el-table-column
          label="姓名"
          prop="name"
          width="100px"
        ></el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          width="60px"
        ></el-table-column>
        <el-table-column prop="age" label="年龄" width="60px"></el-table-column>
        <el-table-column
          label="手机"
          prop="phone"
          width="120px"
        ></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="角色" prop="role">
          <template slot-scope="scope">
              <span :class="{activeFontColor:scope.row.role=='admin'}">{{scope.row.role=='user'?'用户':'管理员'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="照片" prop="photo" width="100px">
          <template slot-scope="scope">
            <img :src="scope.row.photo" height="40px" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button size="mini" @click="openChangeUser(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="delUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        slot="pageSlot"
        :pageNum="pageNum"
        :pageSize="pageSize"
        :totalRows="totalRows"
        @pageChange="pageChange"
      />
    </FrameStyle>
    <!-- 添加用户弹出层 -->
    <el-dialog :visible.sync="flag" width="40%">
      <el-form :model="formData" >
        <el-form-item label="账号">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="formData.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="formData.age"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="formData.gender"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="formData.phone"></el-input>
        </el-form-item>
        <el-form-item label="照片">
          <UploadImg :type="`USER`" @getImgUrl="getImgUrl" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="formData.email"></el-input>
        </el-form-item>
      </el-form>
      <div>
          <el-button type="primary" @click="addUser">确认</el-button>
          <el-button>重置</el-button>
      </div>
    </el-dialog>
    <!-- //修改用户弹出层 -->
    <el-dialog :visible.sync="changeDiaLogVisible" width="40%">
      <el-form :model="changeForm" ref="ruleForm">
        <el-form-item label="账号" prop="username">
          <el-input v-model="changeForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="changeForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="changeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="changeForm.age"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="changeForm.gender"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="changeForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="照片" prop="photo">
          <UploadImg :type="`USER`" @getImgUrl="getImgUrl" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="changeForm.email"></el-input>
        </el-form-item>
      </el-form>
      <div>
          <el-button type="primary" @click="changeUser">确认</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqYongHuList,
  reqAddData,
  reqDelData,
  reqChangeData,
  reqSearchData
} from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      formData: {
        username: "",
        password: "",
        name: "",
        age: "",
        gender: "",
        phone: "",
        email: "",
        photo: "",
        create_time:''
      },
      changeForm: {},
      pageNum: 1,
      pageSize: 10,
      imgUrl: "",
      totalRows: 1,
      flag: false,
      changeDiaLogVisible: false,
      searchVal:''
    };
  },
  created() {
    this.getData();
  },
  methods: {

    //获取用户列表数据
    async getData() {
      let { pageNum, pageSize } = this;
      let result = await reqYongHuList({ pageNum, pageSize });
      if (result.code != 200) return false
      result.data.list.forEach(item=>{
        item.createTime = this.$format(item.createTime)
      })
      this.tableData = result.data.list;
      this.totalRows = result.data.total;
    },
    //添加用户
    async addUser() {
      this.formData.photo = this.imgUrl;
      let result = await reqAddData("/user/add", this.formData);
      if (result.code != 200) return this.$message.error("添加失败");
      this.$message({ message: "添加成功", type: "success" });
      this.getData();
      this.flag = false;
    },
    //打开编辑用户弹出层
    openChangeUser(row) {
      this.changeDiaLogVisible = true;
      delete row.createTime
      this.$nextTick(()=>{
        this.changeForm = row;
      })
    },
    //编辑用户
    async changeUser() {
      this.imgUrl != "" ? (this.changeForm.photo = this.imgUrl) : false;
      let result = await reqChangeData("/user/update", this.changeForm);
      if (result.code != 200) return this.$message.error("修改失败");
      this.$message({ message: "修改成功", type: "success" });
      this.getData();
      this.changeDiaLogVisible = false;
    },
    //删除用户
    async delUser(id) {
      let result = await reqDelData("/user/delete", id);
      if (result.code != 200) return this.$message.error("删除失败");
      this.$message({ message: "删除成功", type: "success" });
      this.getData();
    },
    //搜索用户
    async searchData(){
      let result = await reqSearchData('/user/search',this.searchVal)
    },
    //接收上传图片路径
    getImgUrl(url) {
      this.imgUrl = url;
    },
    //重置表单
    resetForm(formName){
      this.$refs[formName].resetFields()
    },
    //分页器事件
    pageChange(page) {
      this.pageNum = page;
      this.getData();
    },
  },
};
</script>

<style lang="less" scoped>
  .activeFontColor{
    color: rgb(13, 168, 13);
  }
</style>